package com.farm.wcp.onlineController;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

























import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.farm.authority.domain.User;
import com.farm.wcp.entity.Path;
import com.farm.wcp.online.config.SaveZipFile;
import com.farm.wcp.onlineService.onlineInterface;
import com.farm.wcp.pojo.FileInformation;
import com.farm.wcp.pojo.FileType;
import com.farm.web.WebUtils;

@Controller
@RequestMapping("webonline")
@SessionAttributes(types=User.class,value="USEROBJ")
public class IntoSystemController extends WebUtils {
	@Resource
	private onlineInterface  fileInformationServiceImpl;
	@Resource
	private onlineInterface  fileTypeServiceImpl;
	//private PrintWriter writer;
	@RequestMapping("Pubhome")
	public ModelAndView intoHome(ModelAndView modelAndView){		
		modelAndView.setViewName("online/home");
		return modelAndView;
	}
	@RequestMapping("publish")
	public String publish(HttpSession  session){
		
		return "online/publish";
	}
//	//不分类的获取总数
//	@RequestMapping("/PubPageCountData")
//	@ResponseBody
//	public List<FileInformation>   getPageFileInformation(){	
//		System.out.println(1111);
//		return	 fileInformationServiceImpl.select();
//	}
	//分类获取总数
	@RequestMapping("/PubPageTypeCountData")
	@ResponseBody
	public List<FileInformation>   getPageTypeFileInformation(@RequestParam(required=false,value="type") Integer type){						                 		
		
		if(type==null)		
			return	 fileInformationServiceImpl.select();	
		else
			return	 fileInformationServiceImpl.select(type);
	}
	//不分类的获取
//	@RequestMapping("/PubPageData/{page}/{size}")
//	@ResponseBody
//	public List<FileInformation>   getPageFileInformation(@PathVariable(value="page") int page,@PathVariable(value="size") int size,HttpServletResponse response){						                 		
//		return	 fileInformationServiceImpl.select(size, page);
//	}
	//分类获取
	@RequestMapping("/{page}/{size}/PubPageTypeData")
	@ResponseBody
	public List<FileInformation>   getPageTypeFileInformation(@RequestParam(value="type",required=false) 	Integer type, @PathVariable(value="page") int page,@PathVariable(value="size") int size,HttpServletResponse response){						                 		
		if(type==null)
		return	 fileInformationServiceImpl.select(size, page);
		else
			return fileInformationServiceImpl.select(size, page,type);
	}
	//分类获取
		@RequestMapping("/{page}/{size}/{type}/PubGetTypePageType")		
		public ModelAndView   getPageTypeFileInformation(ModelAndView modelAndView,@PathVariable(value="type") Integer type, @PathVariable(value="page") int page,@PathVariable(value="size") int size){						                 								
				
			List<FileInformation> fileInformations= fileInformationServiceImpl.select(size, page,type);
				modelAndView.addObject("fileInformations", fileInformations);
				modelAndView.setViewName("online/filelist");
				return modelAndView;
		}
	@Resource
	private SaveZipFile saveZipFile;
	@RequestMapping("/PubUpload")
	public ModelAndView fileUpload(ModelAndView modelAndView, @RequestParam(value="filetype")Integer type,FileInformation fileInformation,HttpServletRequest request,HttpServletResponse response,@RequestParam(value="file",required=false) CommonsMultipartFile multipartFile) {		
		User user=(User) request.getSession().getAttribute("USEROBJ");
		String serverPath=request.getSession().getServletContext().getRealPath("/");				
		if(saveZipFile.save(serverPath,multipartFile))
		{			
			fileInformation.setType(type);
			fileInformation.setAuthor(user.getName());		
			fileInformation.setcTime(new Date());
			fileInformation.setUrl(saveZipFile.getIndexPath());			
			fileInformation.setZipUrl(saveZipFile.getZipPath());
			fileInformationServiceImpl.save(fileInformation);	
			modelAndView.addObject("indexPath", saveZipFile.getIndexPath());
			modelAndView.addObject("status", 100);
		}
		else{
			modelAndView.addObject("msg", "文件上传失败，请检查格式");
			modelAndView.addObject("status", 200);
		}
		modelAndView.setViewName("online/look");
		return modelAndView;	
}
	@RequestMapping("/PubgetType")
	public @ResponseBody List<FileType> types(){
		return fileTypeServiceImpl.select();	
	}
//	public List<Path> getPath(File file,String p){
//		
//		List<Path> paths=new ArrayList<Path>();	
//		Path parent=new Path();
//		parent.setName(file.getName());
//	//	parent.setUrl(url);
//			for(File f:file.listFiles()){
//				Path path=new Path();		
//				path.setName(f.getName());
//				path.setUrl(p+":"+path.getName());
//				path.setParent(parent);
//				paths.add(path);							
//			}
//			return paths;
//	}
//	@RequestMapping("/PubGetFile/{url}")
//	public void getFile(@PathVariable(value="url") String url){
//		String p=url.replace("", newChar)		
//	}
//	
	//文件路径的“/”的替换
	public String toChange(String url){
		String u=url.replace("/", ":");			
		return u.replace(File.separator, ":");
	}
	//文件路径的“/”的替换的反转
	public String fromChange(String path){
		return path.replace(":", File.separator);
	}
	
}
